package com.gopay.common.oldmermove.dao.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.oldmermove.OldMerMoveStatus;
import com.gopay.common.constants.oldmermove.OldMerTranEffectStatus;
import com.gopay.common.domain.oldmermove.OldMovedMer;
import com.gopay.common.oldmermove.dao.OldMovedMerDAO;
/**
 * 老平台迁移商户记录
 * @OldMovedMerDAOImpl.java
 * @author lizheng
 * @2014-7-3 下午4:24:24  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("oldMovedMerDAO")
public class OldMovedMerDAOImpl extends BaseDAOImpl<OldMovedMer, Serializable> implements OldMovedMerDAO {

	/**
	 * 根据老平台商户ID查询
	 */
	@Override
	public OldMovedMer getByOldMerId(String oldMerId) {
		return this.find(OldMovedMer.class, oldMerId);
	}

	/**
	 * 根据新平台商户ID查询
	 */
	@Override
	public OldMovedMer getByNewMerId(String newMerId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(OldMovedMer.class);
		criteria.add(Restrictions.eq("newMerId", newMerId));
		List<OldMovedMer> list = this.findByCriteria(criteria);
		if(list != null && list.size() == 1) {
            return list.get(0);
        }
		return null;
	}

	/**
	 * 根据旧平台商户ID，查询是否设定生效
	 */
    @Override
    public Map<String, OldMerTranEffectStatus> findSetupEffectByOldMerId(List<String> oldMerIdArray) {
        DetachedCriteria criteria = DetachedCriteria.forClass(OldMovedMer.class);
        criteria.add(Restrictions.in("oldMerId", oldMerIdArray));
        List<OldMovedMer> list = this.findByCriteria(criteria);
        Map<String, OldMerTranEffectStatus> setupEffectMap = new HashMap<String, OldMerTranEffectStatus>();
        for(OldMovedMer mer : list) {
            setupEffectMap.put(mer.getOldMerId(), mer.getTranEffectStatus());
        }
        return setupEffectMap;
    }

    /**
     * 分页查询已已经数据迁移的商户数据
     */
    @Override
    public List<?> findHaveMovedMerInfo(Map<String, String> args, Integer pageIndex, Integer pageSize) {
        StringBuilder sb = new StringBuilder(512);
        sb.append(
                "select o.oldMerId,o.newMerId,c.merName,c.corpName,c.email,o.oldVcardNo,o.tranEffectStatus,o.oldEmail from OldMovedMer o,CustCorpInfo c where o.newMerId = c.custId and o.moveStatus = ")
                .append(OldMerMoveStatus.SUCC.value);
        /* 拼装查询条件 */
        Map<String, Object> param = new HashMap<String, Object>();
        buildHaveMovedMerConditionSql(args, sb, param);
        return this.findByHql(sb.toString(), param, pageIndex, pageSize);
    }
    
    /**
     * 分页查询已已经数据迁移的商户数据条数
     */
    @Override
    public int findCountHaveMovedMerInfo(Map<String, String> args) {
        StringBuilder sb = new StringBuilder(512);
        sb.append(
                "select count(*) from OldMovedMer o,CustCorpInfo c where o.newMerId = c.custId and o.moveStatus = ")
                .append(OldMerMoveStatus.SUCC.value);
        /* 拼装查询条件 */
        Map<String, Object> param = new HashMap<String, Object>();
        buildHaveMovedMerConditionSql(args, sb, param);
        return this.findCountByHql(sb.toString(), param);
    }
    
    /**
     * 拼装 已数据迁移商户 分页查询条件
     */
    private void buildHaveMovedMerConditionSql(Map<String, String> args, StringBuilder sb, Map<String, Object> param) {
        /* email */
        String email = args.get("email");
        if(StringUtils.isNotEmpty(email)) {
        	sb.append(" and o.oldEmail = :email");
            param.put("email", email);
        }
        /* 商户名称 */
        String merName = args.get("merName");
        if(StringUtils.isNotEmpty(merName)) {
            sb.append(" and c.merName = :merName");
            param.put("merName", merName);
        }
        /* 老平台商户ID */
        String oldMerId = args.get("merId");
        if(StringUtils.isNotEmpty(oldMerId)) {
            sb.append(" and o.oldMerId = :oldMerId");
            param.put("oldMerId", oldMerId);
        }
        /* 是否已设定生效时间 */
        String setupEffect = args.get("setupEffect");
        if(StringUtils.isNotEmpty(setupEffect)) {
        	if("y".equals(setupEffect)) {
        		sb.append(" and o.tranEffectStatus <> ").append(OldMerTranEffectStatus.UN_SET.value);
        	} else if ("n".equals(setupEffect)) {
        		sb.append(" and o.tranEffectStatus = ").append(OldMerTranEffectStatus.UN_SET.value);
        	}
        }
    }

    /**
     * 根据参数查询迁移商户数据
     */
    @Override
    public List<?> findOldMerMovedInfoByParam(Map<String, String> args) {
        StringBuilder sb = new StringBuilder(512);
        sb.append(
                "select o.oldMerId,o.newMerId,c.merName,c.corpName,c.email,o.oldVcardNo,o.tranEffectStatus,o.oldEmail from OldMovedMer o,CustCorpInfo c where o.newMerId = c.custId and o.moveStatus = ")
                .append(OldMerMoveStatus.SUCC.value);
        /* 拼装查询条件 */
        Map<String, Object> param = new HashMap<String, Object>();
        buildHaveMovedMerConditionSql(args, sb, param);
        return this.findByHql(sb.toString(), param);
    }

    /**
     * 更新集合数据的tranEffectStatus属性
     */
	@Override
	public void updateTranEffectStatusByMerSet(Collection<OldMovedMer> merSet, OldMerTranEffectStatus effectStatus) {
		for(OldMovedMer mer : merSet) {
			mer.setTranEffectStatus(effectStatus);
			this.update(mer);
		}
		
	}

	/**
	 * 根据旧平台企业客户号查询
	 */
	@Override
	public OldMovedMer getOldMovedMerByCustomerId(String customerId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(OldMovedMer.class);
		criteria.add(Restrictions.eq("customerId", customerId));
		List<OldMovedMer> list = this.findByCriteria(criteria);
		if(list != null && list.size() == 1) {
            return list.get(0);
        }
		return null;
	}
}
